#ifndef THREAD_H
#define THREAD_H

#include <memory>

namespace base
{
class ThreadData;
/**
 * 线程简易封装，继承并重写run()方法即可
 *
 * @author: haitao.lin
 */
class Thread
{
public:
    static unsigned long int currentThreadId() noexcept;

    /**
     * Forces the current thread to sleep for secs seconds.
     */
    static void sleep(unsigned long secs);

    /**
     * Forces the current thread to sleep for msecs milliseconds.
     */
    static void msleep(unsigned long msecs);

    /**
     * Forces the current thread to sleep for usecs microseconds.
     */
    static void usleep(unsigned long usecs);

public:
    Thread();
    explicit Thread(const std::string &thread_name);
    virtual ~Thread();

    /**
     * Begins execution of the thread by calling run().
     * The operating system will schedule the thread.
     * If the thread is already running, this function does nothing.
     */
    void start();

    /**
     * Returns true if the thread is running; otherwise returns false
     */
    bool isRunning() const;

    void setThreadName(const std::string &thread_name);
    std::string getThreadName() const;

protected:
    /**
     * The starting point for the thread.
     * After calling start(), the newly created thread calls this function.
     * You can reimplement this function to facilitate advanced thread management.
     * Returning from this method will end the execution of the thread.
     */
    virtual void run() = 0;

protected:
    friend class ThreadData;
    std::unique_ptr<ThreadData> data_;
};
}

#endif //THREAD_H
